package com.onpositive.mediawiki.serializing;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;

import com.onpositive.data.IPropertyConstants;
import com.onpositive.data.IPropertyMetadata;
import com.onpositive.knowledge.model.impl.BasicUniverseProperty;
import com.onpositive.knowledge.model.impl.MergingProperty;
import com.onpositive.knowledge.model.value.audits.AbstractAuditResult;
import com.onpositive.semantic.model.api.meta.IMeta;
import com.onpositive.semantic.model.api.property.IProperty;
import com.onpositive.units.ParsedDate;
import com.onpositive.units.ParsedDimension;
import com.onpositive.units.ParsedFileList;
import com.onpositive.units.ParsedIdent;
import com.onpositive.units.ParsedLinkArray;
import com.onpositive.units.ParsedScalar;
import com.onpositive.units.ParsedSection;
import com.onpositive.units.ParsedString;



public class ParsedPropertyMetadata implements IPropertyMetadata {
	
	private static HashMap<String, Class<?>> typeMap ;
	static{
		typeMap = new HashMap<String, Class<?>>();
		typeMap.put( ParsedDimension.ID , Double.class   ) ;
		typeMap.put( ParsedScalar.ID    , Double.class	 ) ;
		typeMap.put( ParsedDate.ID 	    , Date.class	 ) ;
		typeMap.put( ParsedIdent.ID	    , String.class   ) ;
		typeMap.put( ParsedString.ID	, String.class   ) ;
		typeMap.put( ParsedSection.ID   , String.class   ) ;
		typeMap.put( ParsedLinkArray.ID , String.class   ) ;
		typeMap.put( ParsedFileList.ID  , String.class   ) ;
	}
	
	private IProperty prop ;
	private IMeta meta ;
	
	public IProperty getProperty() {
		return prop;
	}

	public ParsedPropertyMetadata( IProperty prop, AbstractAuditResult result )
	{
		if( prop instanceof BasicUniverseProperty )
			this.prop = new MergingProperty ( prop, result ) ;
		else
			this.prop = prop ;

		this.meta = prop.getMeta() ;
	}

	@Override
	public String id() {
		return meta.getSingleValue( IPropertyConstants.CHAR_PROPERTY_NAME_LABEL, String.class, null ) ;
	}

	@Override
	public Collection<String> keys() {
		
		Collection<Object> metaKeys = meta.keys() ;
		ArrayList<String> result = new ArrayList<String>( metaKeys.size() ) ;
		for( Object obj : metaKeys )
			result.add( ( obj instanceof String ) ? (String) obj : obj.toString() ) ;
		
		return result ;
	}

	@Override
	public Class<?> type() {
		String parserId = meta.getSingleValue( IPropertyConstants.CHAR_PARSER_ID_LABEL, String.class, null );
		return typeMap.get( parserId ) ;
	}

	@Override
	public Object getMetaPropertyValue(String key)
	{
		String stringValue = meta.getSingleValue( key, String.class, null );
		if( stringValue == null ){
			Boolean booleanValue = meta.getSingleValue( key, Boolean.class, null );
			if( booleanValue != null )
				stringValue = booleanValue.toString() ;
		}		
		return stringValue;
	}

	
	@Override
	public Object getValue(Object obj) {
		if (this.prop.getId().equals("origin")&&obj.toString().startsWith("Leopard")){
			System.out.println("A");
		}
		return prop.getValue(obj);
	}

}
